package etnorservice.etnor_service.service.impl;

import etnorservice.etnor_service.domain.ProblemDO;
import etnorservice.etnor_service.domain.po.Problem;
import etnorservice.etnor_service.domain.po.User;
import etnorservice.etnor_service.domain.response.ProblemResponse;
import etnorservice.etnor_service.domain.vo.ProblemSubmitVO;
import etnorservice.etnor_service.domain.vo.ProblemVO;
import etnorservice.etnor_service.domain.vo.TicktsVO;
import etnorservice.etnor_service.mapper.UserMapper;
import etnorservice.etnor_service.request.ProblemSearchRequest;
import etnorservice.etnor_service.request.ProblemSubmitRequest;
import etnorservice.etnor_service.service.ProblemService;
import etnorservice.etnor_service.utils.UniqueIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: cc
 * @Date: 2024/08/29/13:56
 **/
@Service
public class ProblemServiceImpl implements ProblemService {
    @Autowired
    UserMapper userMapper;
    @Override
    public ProblemSubmitVO getBaseParm() {
        ProblemSubmitVO problemSubmitVO=new ProblemSubmitVO();
        problemSubmitVO.setAdmins(userMapper.findSuperAdmin());
        problemSubmitVO.setProblemList(userMapper.findProblemKinds());
        return problemSubmitVO;
    }

    @Override
    public int insertProblem(ProblemSubmitRequest problemSubmitRequest) {
        ProblemDO problemDO=new ProblemDO();
        problemDO.setAdminid(Integer.parseInt(problemSubmitRequest.getAdminId()+""));
        problemDO.setDescription(problemSubmitRequest.getDescription());
        problemDO.setIssuetype(problemSubmitRequest.getIssueType());
        problemDO.setNote(problemSubmitRequest.getNote());
        problemDO.setUrgency(problemSubmitRequest.getUrgency());
        problemDO.setProblemid(UniqueIdGenerator.generateUUID());
        problemDO.setTalentid(problemSubmitRequest.getTalentId());
        problemDO.setCreatetime(new Date());
        problemDO.setState(1);
        problemDO.setFinished(0);
        problemDO.setReqId(problemSubmitRequest.getReq_id());
        try {
            userMapper.insertProblem(problemDO);
        }catch (Exception e){
            e.printStackTrace();
        }

        return 0;
    }

    @Override
    public List<ProblemResponse> problems(Integer talentId) {
        List<ProblemResponse> problemResponseList = new ArrayList<>();
        List<ProblemDO> problemByTalentId = userMapper.findProblemByTalentId(talentId);
        for(ProblemDO problemDO:problemByTalentId){
            ProblemResponse problemResponse=new ProblemResponse();
            problemResponse.setName(
                    userMapper.findRoleByAccount(problemDO.getAdminid()
                    ).getUsername());
            problemResponse.setCreateTime(problemDO.getCreatetime());
            problemResponse.setFinished(problemDO.getFinished());
            problemResponse.setUrgency(problemDO.getUrgency());
            problemResponse.setFininshTime(problemDO.getFinishtime());
            problemResponse.setDescription(problemDO.getDescription());
            problemResponse.setProblemid(problemDO.getProblemid());
            problemResponseList.add(problemResponse);
        }

        return problemResponseList;
    }

    @Override
    public int cancelProblem(String problemid) {
        return userMapper.cancelProblem(problemid);
    }

    @Override
    public List<ProblemVO> getProblemList() {
        List<ProblemVO> problemVOS=new ArrayList<>();
        List<Problem> problemList = userMapper.findProblemList();
        for(Problem problem:problemList){
            ProblemVO problemVO= new ProblemVO();
            problemVO.setId(problem.getId());
            problemVO.setKind(problem.getKind());
            problemVOS.add(problemVO);
        }
        return problemVOS;
    }

    @Override
    public List<TicktsVO> findProblemsByConditions(ProblemSearchRequest problemSearchRequest) {
        List<ProblemDO> problemsByConditions = userMapper.findProblemsByConditions(problemSearchRequest);
        List<TicktsVO> ticktsVOS=new ArrayList<>();
        for (ProblemDO problemDO : problemsByConditions){
            TicktsVO ticktsVO = new TicktsVO();
            ticktsVO.setCreateTime(problemDO.getCreatetime());
            ticktsVO.setDescription(problemDO.getDescription());
            ticktsVO.setFinishTime(problemDO.getFinishtime());
            ticktsVO.setStatus(problemDO.getState());
            ticktsVO.setUrgency(problemDO.getUrgency());
            ticktsVO.setFinished(problemDO.getFinished());
            ticktsVO.setPloblemId(problemDO.getProblemid());
            User roleByAccount = userMapper.findRoleByAccount(problemDO.getTalentid());
            ticktsVO.setUserName(roleByAccount.getUsername());
            ticktsVO.setTitle(problemDO.getTitle());
            ticktsVOS.add(ticktsVO);
        }



        return ticktsVOS;
    }
}
